package util

import (
	"fmt"
	"os"
	"time"

	"github.com/gookit/color"
)

func PrintEndFail(err string) {
	color.Redp(fmt.Sprintf("|* %s \n", err))
	os.Exit(1)
}

func PrintFail(err string) {
	color.Redp(fmt.Sprintf("|* %s \n", err))
}

func PrintFails(errList []string) {
	for _, err := range errList {
		PrintFail(err)
	}
}

func PrintError(err error) {
	color.Redp(fmt.Sprintf("|* error: %s \n", err.Error()))
	os.Exit(1)
}

func PrintSuccess(input string) {
	color.Greenp(fmt.Sprintf("|* success: %s \n", input))
}

// ReadTime 格式化时间为易读格式
func ReadTime(elapsedTime int64) string {
	duration := time.Duration(elapsedTime) * time.Millisecond
	hours := duration / time.Hour
	duration -= hours * time.Hour

	minutes := duration / time.Minute
	duration -= minutes * time.Minute

	seconds := duration / time.Second
	duration -= seconds * time.Second
	elapsedTime = duration.Milliseconds()
	result := ""
	if hours > 0 {
		result += fmt.Sprintf("%d小时", hours)
	}
	if minutes > 0 {
		result += fmt.Sprintf("%d分钟", minutes)
	}
	if seconds > 0 {
		result += fmt.Sprintf("%d秒", seconds)
	}
	if elapsedTime > 0 || result == "" {
		result += fmt.Sprintf("%d毫秒", elapsedTime)
	}
	return result
}
